//
//  BVMineTool.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/12.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVMineTool.h"
#import "BVVerifyCode.h"
#import "BVMyCouponModel.h"
#define KEY_CODE @"code"
#define SUCCESS_CODE @"0"

@interface BVMineTool ()

@end

@implementation BVMineTool

- (void)fetchVerifyCode:(NSString *)mobile success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_get_verifycode",PATH_TEST];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = mobile;
    
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {
        
        BVVerifyCode *verifycode = [BVVerifyCode mj_objectWithKeyValues:responseObject[@"result"]];
        
        if (success) {
            success(verifycode.status);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)registered:(NSString *)mobile verify_code:(NSString *)verify_code password:(NSString *)password success:(void (^)(NSMutableDictionary *))success failure:(void (^)(NSError *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_user_register",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = mobile;
    params[@"verify_code"] = verify_code;
    params[@"password"] = password;
    params[@"invite_code"] = @"";
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {

        dic[@"code"] = responseObject[@"code"];
        dic[@"msg"] = responseObject[@"msg"];
        NSDictionary *dict = responseObject[@"reslut"];
        self.user.user_id = dict[@"id"];
        
        if (success) {
            success(dic);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)loginByMobile:(NSString *)mobile password:(NSString *)password success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_user_login",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = mobile;
    params[@"password"] = password;
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {

        success(responseObject[@"code"]);
        if ([responseObject[@"code"] isEqualToString:@"0"]) {
            NSDictionary *dict = responseObject[@"result"];
            self.user.user_id = dict[@"id"];
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)logoutSuccess:(void (^)(NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_user_logout",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {
        if (success) {
            success(responseObject[@"code"]);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)settingUserAvatar:(UIImage *)image success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_upload_avatar",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    [BVHttpRequest uploadImageWithUrlString:url image:image parameters:params success:^(id responseObject) {
        NSDictionary *dict = responseObject[@"result"];
        self.user.avatar_url = dict[@"avatar"];
        success();
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)modifyUserAddress:(NSString *)address detailAddr:(NSString *)detailAddr provId:(NSString *)provId cityId:(NSString *)cityId success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_modify_useraddress",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"address"] = detailAddr;
    params[@"province_id"] = provId;
    params[@"city_id"] = cityId;
    params[@"user_id"] = self.user.user_id;
//    NSLog(@"%@-%@",params,address);
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {
        if ([responseObject[KEY_CODE] isEqualToString:SUCCESS_CODE]) {
            self.user.address = address;
            self.user.detail_address = detailAddr;
            self.user.addr_cityId = cityId;
            self.user.addr_provinceId = provId;
        }
        success();
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)fetchUserInfoSuccess:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_get_userinfo",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if ([responseObject[KEY_CODE] isEqualToString:SUCCESS_CODE]) {
            NSDictionary *dict = responseObject[@"result"];
            self.user.mobile = dict[@"mobile"];
            self.user.name = dict[@"username"];
            self.user.avatar_url = dict[@"avatar"];
            self.user.gender = [dict[@"sex"] isEqualToString:@"男"] ? 1:[dict[@"sex"] isEqualToString:@"女"] ? 2:3;
            self.user.birthday = dict[@"birthday"];
            self.user.address = [NSString stringWithFormat:@"%@-%@",dict[@"province_name"],dict[@"city_name"]];
            self.user.detail_address = dict[@"address"];
            self.user.addr_cityId = dict[@"city_id"];
            self.user.addr_provinceId = dict[@"province_id"];
        }
        success();
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];//http://121.42.189.14:808220161124/thumb_201611240448279277.png"
}

- (void)modifyUserBirthday:(NSString *)birthday success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_modify_userbirthday",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    params[@"birthday"] = birthday;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if (success) {
            success();
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)modifyUserName:(NSString *)name success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_modify_username",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    params[@"new_name"] = name;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if (success) {
            success();
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)modifyUserMobile:(NSString *)mobile success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_modify_usermobile",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    params[@"mobile"] = mobile;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if (success) {
            success();
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)modifyUserSex:(NSInteger)sex success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_modify_usersex",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    params[@"sex"] = [NSString stringWithFormat:@"%ld",sex];
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if (success) {
            success();
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
- (void)fetchUserCoupon:(NSString *)type success:(void (^)(NSMutableArray *))success failure:(void (^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/UserAPI/c_get_usercoupon",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    params[@"type"] = type;
    
    NSMutableArray *arr = [NSMutableArray array];
    [BVHttpRequest postWithUrlString:url parameters:params success:^(id responseObject) {
        NSLog(@"----%@",responseObject);
        if ([responseObject[KEY_CODE] isEqualToString:SUCCESS_CODE]) {
            for (NSMutableDictionary *dic in responseObject[@"result"]) {
                BVMyCouponModel *myCouponModel = [[BVMyCouponModel alloc]init];
                myCouponModel.condition = dic[@"condition"];
                myCouponModel.coupon_id = dic[@"coupon_id"];
                myCouponModel.end_time = dic[@"end_time"];
                myCouponModel.money = dic[@"money"];
                myCouponModel.start_time = dic[@"start_time"];
                myCouponModel.typ = dic[@"typ"];
                [arr addObject:myCouponModel];
            }
        }
        if (success) {
            success(arr);
        }
    } failure:^(NSError *error) {
        
    }];
}

+ (void)fetchMineData:(void (^)(NSArray *))success {
    
    NSArray *array = [NSArray array];
    NSArray *first = @[@{@"image":@"wodedingdan",@"title":@"我的订单"},
                       @{@"image":@"wodeshoucang",@"title":@"我的关注"},
                       @{@"image":@"shouhuodizhi",@"title":@"收获地址"},
                       ];
    NSArray *second = @[@{@"image":@"wodeqianbao",@"title":@"我的钱包"},
                        @{@"image":@"youhuijuan",@"title":@"优惠券"},
                        @{@"image":@"jifenshangcheng",@"title":@"积分商城"},
                        @{@"image":@"huiyuanzhongxin",@"title":@"会员中心"},
                        @{@"image":@"tuijianyoujiang",@"title":@"推荐有奖"}
                        ];
    NSArray *third = @[@{@"image":@"lianxikefu",@"title":@"联系客服"},
                       @{@"image":@"fuwuxuzhi",@"title":@"服务须知"},
                       @{@"image":@"shezhi",@"title":@"设置"},
                       @{@"image":@"yijianfankui",@"title":@"意见反馈"}
                       ];
    array = @[first,second,third];
    success(array);
}


@end
